﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Net;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES1008.Services
{
    public class ServiceMethod
    {
        public static RestClient _restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);

        public static ExecutionResult<dynamic> GetKPNPrefixList(string vKPNPrefix=null,string vCustCode = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/prefix/getlist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vKPNPrefix))
                {
                    _request.AddQueryParameter("line", vKPNPrefix);
                }
               
                if(!string.IsNullOrEmpty(vCustCode))
                {
                    _request.AddQueryParameter("shift", vCustCode);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取料头信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> AddKPNPrefix(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/prefix/add";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "新增料头信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> UpdateKPNPrefix(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/prefix/update";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);
                    
                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "更新料头信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> DeleteKPNPrefix(string vId)
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            string api_path = $"/basicdata/prefix/delete/{vId}";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);                

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "删除料头信息异常." + ex.Message;
            }

            return result;
        }
    }
}
